package com.spzx.user.controller;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import com.spzx.user.domain.UserAddress;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.annotation.Resource;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.user.service.IUserAddressService;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 用户地址Controller
 *
 * @author atguigu
 * @date 2024-07-10
 */
@Tag(name = "用户地址接口管理")
@RestController
@RequestMapping("/userAddress")
public class UserAddressController extends BaseController
{
    @Resource
    private IUserAddressService userAddressService;
    @Operation(summary = "内部调用接口:根据id查询收货地址详情")
    @InnerAuth
    @GetMapping("getAddress/{id}")
    public R<UserAddress> getAddress(@PathVariable("id")Long id){
        return R.ok(userAddressService.getById(id));
    }

    //
    ///userAddress/list
    @Operation(summary = "新增收货地址")
    @PostMapping
    public AjaxResult saveAddress(@RequestBody UserAddress userAddress){

        return toAjax( userAddressService.saveAddress(userAddress));
    }
    @Operation(summary = "查询登录用户收货地址")
    @GetMapping("/list")
    public AjaxResult list(){
        List<UserAddress> userAddresses = userAddressService.list(Wrappers.lambdaQuery(UserAddress.class)
                .eq(UserAddress::getUserId, SecurityContextHolder.getUserId()));
        return success(userAddresses);
    }

}
